#ifndef FADAPTER_H
#define FADAPTER_H

#include <QObject>

#include <fclient/fclient.h>
#include <f/ferrors.h>

class FAdapter : public QObject
{
    Q_OBJECT

    static struct _flight_telemetry {
        float lat;
        float lon;
        float alt;
        float roll;
        float pitch;
        float head;
        float vel;
        float b1v;
        float b2v;
        float frames;
    } ftm;

    struct _fc_tlm_handle *ftm_handle;

    static fc_instance_t fclient;

    static FAdapter* m_fadapter;

    static QString command_responce;

    explicit FAdapter(QObject *parent = 0);
public:
    static FAdapter* instance()
    {
        if(!m_fadapter)
            m_fadapter = new FAdapter;

        return m_fadapter;
    }

    bool finit(char *);

    static int read_telemetry( void *d, int n );

    int handler_ftm_new (ftm_data_storage_t *d)
    {
        emit _ftm_new (d);
        return 0;
    }

    int handler_ftm_reset ( ftm_data_storage_t *d )
    {
       emit _ftm_new (d);
        return 0;
    }
    int handler_ftm_msg ( ftm_data_storage_t *d, char *msg )
    {
        emit _ftm_msg (d, msg);
       return 0;
    }
    int handler_ftm_abt_req ( ftm_data_storage_t *d )
    {
        emit _ftm_abt_req (d);
       return 0;
    }
    int handler_ftm_err ( ftm_data_storage_t *d, char *msg )
    {
       emit _ftm_err (d, msg);
       return 0;
    }
    int handler_ftm_transmitted ( ftm_data_storage_t *d )
    {
       emit _ftm_transmitted (d);
        return 0;
    }
    int handler_ftm_passed ( ftm_data_storage_t *d )
    {
        emit _ftm_passed (d);
        return 0;
    }
    int handler_ftm_done ( ftm_data_storage_t *d, fc_report_inf_type_t type, char *msg )
    {
        emit _ftm_done (d, type, msg);
        return 0;
    }
    void handler_send_telemetry(const QString& topic, QByteArray ba)
    {
        emit send_telemetry(topic, ba);
    }

    void handler_mission_sync_msg(char* msg)
    {
        emit send_responce(QString(msg));
    }

public slots:
    void slotCommandControl(QByteArray, QString*);

signals:
    void _ftm_new (ftm_data_storage_t *d);
    void _ftm_reset ( ftm_data_storage_t *d );
    void _ftm_msg ( ftm_data_storage_t *d, char *msg );
    void _ftm_abt_req ( ftm_data_storage_t *d );
    void _ftm_err ( ftm_data_storage_t *d, char *msg );
    void _ftm_transmitted ( ftm_data_storage_t *d );
    void _ftm_passed ( ftm_data_storage_t *d );
    void _ftm_done ( ftm_data_storage_t *d, fc_report_inf_type_t type, char *msg );
    void send_telemetry(QString, QByteArray);
    void send_responce(QString);
};



#endif // FADAPTER_H
